package com.itlong.cloud.POJO.PO;

import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Map;
import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

/**
 * <desc>
 *      轮询指令delayQueue数据对象
 * </desc>
 *
 * @createDate 2019/08/01
 */
@Data
@NoArgsConstructor
public class SendPushMessageTaskPO implements Delayed{
    private  String productKey; //设备key
    private  String topic; //指令路由
    private  String deviceUnique;//设备编号
    private  String sourceForm; //命令发起方标识
    private  String cmd; //命令类型
    private  Integer snr; //流水号
    private  String commandType; //指令类型
    private Integer iotFlag;        //iot类型
    private  long delay; //延迟时间
    private  long expire;  //到期时间

    /**
     * <desc>
     *      初始化
     * </desc>
     *
     * @param delay 延迟时间
     * @author Qiang.S
     * @createDate 2019/08/01
     */
    public SendPushMessageTaskPO(String productKey, String topic,String deviceUnique,String sourceForm,String cmd,Integer snr,String commandType,long delay) {
        this.productKey = productKey;
        this.topic = topic;
        this.deviceUnique = deviceUnique;
        this.sourceForm = sourceForm;
        this.cmd = cmd;
        this.snr = snr;
        this.commandType = commandType;
        this.delay = delay;
        this.expire = System.currentTimeMillis() + delay;
    }
    /**
     * <desc>
     *      初始化
     * </desc>
     *
     * @param delay 延迟时间
     * @author Qiang.S
     * @createDate 2019/08/01
     */
    public SendPushMessageTaskPO(String productKey, String topic,String deviceUnique,String sourceForm,String cmd,Integer snr,String commandType,long delay,Integer iotFlag) {
        this.productKey = productKey;
        this.topic = topic;
        this.deviceUnique = deviceUnique;
        this.sourceForm = sourceForm;
        this.cmd = cmd;
        this.snr = snr;
        this.commandType = commandType;
        this.delay = delay;
        this.iotFlag = iotFlag;
        this.expire = System.currentTimeMillis() + delay;
    }
    /**
     * <desc>
     *      延迟时间
     * </desc>
     *
     * @param unit
     * @return
     * @author Qiang.S
     * @createDate 2019/08/01
     */
    @Override
    public long getDelay(TimeUnit unit) {
        return unit.convert(this.expire - System.currentTimeMillis() , TimeUnit.MILLISECONDS);
    }

    /**
     * <desc>
     *      用于延迟队列内部比较排序   当前时间的延迟时间 - 比较对象的延迟时间
     * </desc>
     *
     * @param o
     * @return
     * @author Qiang.S
     * @createDate 2019/08/01
     */
    @Override
    public int compareTo(Delayed o) {
        return (int) (this.getDelay(TimeUnit.MILLISECONDS) -o.getDelay(TimeUnit.MILLISECONDS));
    }
}
